<?php
Globals::requireClass('Controller');
Globals::requireModel('User');
Globals::requireTable('User');
Globals::requireTable('Product');
Globals::requireTable('Order');
Globals::requireTable('Account');
Globals::requireTable('Msg');
Globals::requireTable('Withdrawals');

class PayController extends Controller
{
	protected $UserTable;
	protected $ProductTable;
	protected $OrderTable;
	protected $AccountTable;
	protected $MsgTable;
	protected $WithdrawalsTable;
	
	public static $defaultConfig = array(
		'viewEnabled'	=> false,
		'layoutEnabled'	=> false,
		'title'			=> null
	);
	
	public function __construct($config = null)
	{
	    $config['viewEnabled'] = false;
	    $config['layoutEnabled'] = false;
		parent::__construct($config);
		$this->UserTable          = new UserTable($config);
		$this->ProductTable       = new ProductTable($config);
		$this->OrderTable         = new OrderTable($config);
		$this->AccountTable       = new AccountTable($config);
		$this->MsgTable           = new MsgTable($config);
		$this->WithdrawalsTable   = new WithdrawalsTable($config);
	}
	
	public function indexAction()
	{
		
	}
	
	/**
	 * 下单
	 * uid/token&machine_type
	 * aid
	 */
	public function onedayAction()
	{
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>-1,'orderid'=>''));
	        exit();
	    }
	    
	    $time = time();
	    $aid = $this->getIntParam('aid');
	    $account = $this->AccountTable->getRow($aid);
	    $product = $this->ProductTable->getRow($account['pid']);
	    if(empty($aid) || $account['status'] != 2 || $product['status'] != 1 || $account['nums_stock'] >= $account['nums_value'] || $account['endtime'] <= $time){
	        echo json_encode(array('returncode'=>-2,'orderid'=>''));
	        exit();
	    }
	    
	    $ispayed = $this->OrderTable->getRow(array('uid'=>$uid,'status'=>1,'aid'=>$aid));
	    if($ispayed['id'] > 0){
	        echo json_encode(array('returncode'=>-3,'orderid'=>''));
	        exit();
	    }
	    
	    $time_day = strtotime(date('Y-m-d'));
	    $day_value = ($account['endtime'] - $time_day) / 86400;
	    $day_value = $day_value == (int)$day_value ? $day_value : (int)$day_value + 1;
		
	    $data['orderid'] = substr($time, -9) . mt_rand(10, 99);
	    $data['uid'] = $uid;
	    $data['pid'] = $account['pid'];
	    $data['aid'] = $aid;
	    $data['createtime'] = date('Y-m-d H:i:s');
	    $data['status'] = 0;
	    $data['money'] = $account['money'] * $day_value;
	    $data['days'] = $day_value;
	    $data['endtime'] =$account['endtime'];
	    $data['uid_from'] = $account['uid'];
	    $this->OrderTable->add($data);
	    echo json_encode(array('returncode'=>0,'orderid'=>$data['orderid']));
	    exit();
	}
	
	/**
	 * 支付成功
	 * orderid
	 * payorderid
	 * paytype
	 * sign
	 */
	public function payokAction()
	{
	    $orderid = $this->getParam('orderid');
	    $payorderid = $this->getParam('payorderid');
	    $paytype = $this->getParam('paytype');
	    $sign = $this->getParam('sign');
	    $time = time();
	    
	    $order = $this->OrderTable->getRow(array('orderid'=>$orderid));
	    $account = $this->AccountTable->getRow($order['aid']);
	    $user = $this->UserTable->getRow($account['uid']);//账号归属人
	    
	    if($order['id'] < 1 || $order['status'] != 0 || $account['id'] < 1 || $account['status'] != 2 || $account['nums_stock'] >= $account['nums_value'] || $account['endtime'] < $time){
	        echo json_encode(array('returncode'=>-1,'username'=>'','password'=>''));
	        exit();
	    }
	    
	    $new_num_arr = array();
	    $orderid_num = $orderid + 19921206;
	    for ($ii = 0 ; $ii < strlen($orderid_num) ; $ii++){
	        $tmp_num = substr($orderid_num, $ii , 1);
	        $new_num_arr[] = $tmp_num;
	    }
	    krsort($new_num_arr);
	    $new_num = '';
	    foreach ($new_num_arr as $v){
	        $new_num .= (string)$v;
	    }
	    if($sign != $new_num){
	        echo json_encode(array('returncode'=>-2,'username'=>'','password'=>''));
	        exit();
	    }

	    switch ($paytype){
	        case 'alipay':
	            $paytypevalue = 1;
	            break;
	        default:
	            $paytypevalue = 1;
	            break;
	    }
	    $data['paytype'] = $paytypevalue;
	    $data['paytime'] = date('Y-m-d H:i:s');
	    $data['payorderid'] = $payorderid;
	    $data['status'] = 1;
	    $this->OrderTable->update($data,$order['id']);
	    
	    $account_data['nums_stock'] = $account['nums_stock'] + 1;
	    $this->AccountTable->update($account_data,$account['id']);
	    
	    $user_data['money'] = $user['money'] + $order['money'];
	    $this->UserTable->update($user_data,$user['id']);
	    
	    if($account['nums_stock'] <= $account['nums_show']){
    	    $msg_data['uid'] = $user['id'];
    	    $msg_data['createdate'] = date('Y-m-d H:i:s');
    	    $msg_data['msg'] = '有人购买了你的账号';
    	    $msg_data['status'] = 0;
    	    $this->MsgTable->add($msg_data);
	    }
	    
	    echo json_encode(array('returncode'=>0,'username'=>$account['username'],'password'=>$account['password']));
	    exit();
	}
	
	protected function out()
	{
		parent::out();
	}
}

Config::extend('PayController', 'Controller');
